package com.cskt.itripbiz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskt.common.constants.ErrorCodeEnum;


import com.cskt.common.exception.ServiceException;
import com.cskt.common.vo.AreaDicVO;
import com.cskt.entity.AreaDic;
import com.cskt.itripbiz.service.AreaDicService;
import com.cskt.mapper.AreaDicMapper;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 〈一句话功能简述〉<br>
 * 〈实现类〉
 *
 * @author 86186
 * @create 2020/11/23
 * @since 1.0.0
 */
@Service
public class AreaDicServiceImpl extends ServiceImpl<AreaDicMapper, AreaDic> implements AreaDicService {
    /*** 查询热门城市Id和城市名 * @param type * @return
     * @param type*/
    @Override
    public List<AreaDicVO> findAreaDicVOByType(Integer type) {
/*        QueryWrapper<AreaDic> areaDicQueryWrapper = new QueryWrapper<>();
        areaDicQueryWrapper.select("id","name")
        .eq("is_hot", 1)
        .eq("is_china", type);*/
        LambdaQueryWrapper<AreaDic> areaDicQueryWrapper = new QueryWrapper<AreaDic>().lambda()
                .select(AreaDic::getId, AreaDic::getName)
                .eq(AreaDic::getIsHot, 1)
                .eq(AreaDic::getIsChina, type);
        List<AreaDic> areaDicList = this.list(areaDicQueryWrapper);//this指代什么？
        if (CollectionUtils.isEmpty(areaDicList)) {
            //当数据为空时，直接抛出异常
            throw new ServiceException(ErrorCodeEnum.BIZ_DATA_NULL);
        }
        //完成类型转换  将list转换为steam流
        List<AreaDicVO> areaDicVOList = areaDicList.stream().map(areaDic -> {
            AreaDicVO areaDicVO = new AreaDicVO();
            BeanUtils.copyProperties(areaDic, areaDicVO);
            return areaDicVO;
        }) //将stream转换成为ist
                .collect(Collectors.toList());


        return areaDicVOList;
    }

    @Override
    public List<AreaDicVO> findAreaDicVOByCityId(Long cityId) {
     /*   QueryWrapper<AreaDic> areaDicQueryWrapper = new QueryWrapper<>();
        areaDicQueryWrapper.select("id", "name")
                .eq("is_trading_area", 1)
                .eq("parent", cityId);*/

        LambdaQueryWrapper<AreaDic> areaDicQueryWrapper = new LambdaQueryWrapper<AreaDic>()
                .select(AreaDic::getId, AreaDic::getName)
                .eq(AreaDic::getIsTradingArea, 1)
                .eq(AreaDic::getParent, cityId);
        List<AreaDic> areaList = this.list(areaDicQueryWrapper);
        if (CollectionUtils.isEmpty(areaList)) {
            throw new ServiceException(ErrorCodeEnum.BIZ_DATA_NULL);
        }
        //调用公共方法，完成类型转换
        List<AreaDicVO> areaDicVOList = areaDicToAreaDicvo(areaList);
        return areaDicVOList;
    }
    //类型的数据转换
    public List<AreaDicVO> areaDicToAreaDicvo(List<AreaDic> areaList) {
        //完成类型转换f
        return areaList.stream().map(areaDic -> { AreaDicVO areaDicVO = new AreaDicVO();
            BeanUtils.copyProperties(areaDic, areaDicVO);
            return areaDicVO;
        }).collect(Collectors.toList());
    }
}
